class Solution {
public:
    int maxFrequency(vector<int>& nums, int k) {
        sort(nums.begin(), nums.end());
        long long temp_sum = 0;
        int i = 0, j = 0, ans = 0;
        for(; j < nums.size(); ++j){
            temp_sum += nums[j];
            long long bigboss = nums[j];
            bigboss *= (j - i + 1);
            while(bigboss - temp_sum > k){
                temp_sum -= nums[i++];
                bigboss -= nums[j];
            }
            ans = max(ans, j - i + 1);
        }
        return ans;
    }
};

//2021.4.25
